package com.jichangxiu.server.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jichangxiu.common.annotation.DataScope;
import com.jichangxiu.common.constant.Constant;
import com.jichangxiu.common.entity.bo.PageData;
import com.jichangxiu.common.exception.ServiceException;
import com.jichangxiu.common.utils.StrUtils;
import com.jichangxiu.common.utils.TokenUtils;
import com.jichangxiu.framework.mybatis.BaseServiceImpl;
import com.jichangxiu.server.platform.entity.dto.PlatformPostQueryDto;
import com.jichangxiu.server.platform.entity.map.PlatformPostBasic;
import com.jichangxiu.server.platform.entity.po.PlatformPost;
import com.jichangxiu.server.platform.entity.po.PlatformUserPost;
import com.jichangxiu.server.platform.entity.vo.PlatformPostAddVo;
import com.jichangxiu.server.platform.entity.vo.PlatformPostEditVo;
import com.jichangxiu.server.platform.entity.vo.PlatformPostQueryVo;
import com.jichangxiu.server.platform.mapper.PlatformPostMapper;
import com.jichangxiu.server.platform.service.PlatformPostService;
import com.jichangxiu.server.platform.service.PlatformUserPostService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

/**
 * PlatformPostServiceImpl:
 *
 * @author Jcx
 * @create 2023-03-08 21:18
 */
@Service
public class PlatformPostServiceImpl extends BaseServiceImpl<PlatformPostMapper, PlatformPost> implements PlatformPostService {

    @Resource
    private TokenUtils tokenUtils;

    @Resource
    private StrUtils strUtils;

    @Resource
    private PlatformPostMapper platformPostMapper;

    @Resource
    private PlatformUserPostService platformUserPostService;

    /**
     * 新增岗位
     *
     * @param platformPostAddVo 岗位视图对象
     * @return 是否成功
     */
    @Override
    public Boolean add(PlatformPostAddVo platformPostAddVo) {
        if (ObjectUtil.isEmpty(platformPostAddVo)) {
            throw ServiceException.build("岗位对象无效");
        }

        PlatformPost platformPost = PlatformPostBasic.INSTANCE.toPlatformPost(platformPostAddVo);
        if (ObjectUtil.isEmpty(platformPost)) {
            throw ServiceException.build("岗位对象无效");
        }

        if (Constant.PLATFORM_SUPER_POST.equals(platformPost.getPostCode())) {
            throw ServiceException.build("岗位标识不能为 PLATFORM_SUPER_POST");
        }

        if (Constant.SYSTEM_SUPER_POST.equals(platformPost.getPostCode())) {
            throw ServiceException.build("岗位标识不能为 SYSTEM_SUPER_POST");
        }

        // Code 必须唯一
        PlatformPost dbPlatformPostByCode = getOne(new LambdaQueryWrapper<PlatformPost>().eq(PlatformPost::getDeptId, platformPost.getDeptId()).eq(PlatformPost::getPostCode, platformPost.getPostCode()));
        if (ObjectUtil.isNotEmpty(dbPlatformPostByCode)) {
            throw ServiceException.build(platformPost.getPostName() + "该岗位的标识重复");
        }

        // name 必须唯一
        PlatformPost dbPlatformPostByName = getOne(new LambdaQueryWrapper<PlatformPost>().eq(PlatformPost::getDeptId, platformPost.getDeptId()).eq(PlatformPost::getPostName, platformPost.getPostName()));
        if (ObjectUtil.isNotEmpty(dbPlatformPostByName)) {
            throw ServiceException.build(platformPost.getPostName() + "该岗位的名称重复");
        }

        platformPost.setPostNo(strUtils.createNo("post"));
        platformPost.setTenantId(tokenUtils.getTenantId());

        return save(platformPost);
    }

    /**
     * 多选级联删除岗位
     *
     * @param postIdList 岗位ID列表
     * @return 是否成功
     */
    @Override
    public Boolean del(List<String> postIdList) {
        List<PlatformPost> platformPostList = list(new LambdaQueryWrapper<PlatformPost>().in(PlatformPost::getPostId, postIdList));
        if (ObjectUtil.isEmpty(platformPostList)) {
            throw ServiceException.build("数据库中不存在需要删除的记录");
        }

        platformPostList.forEach(platformPost -> {
            if (Constant.PLATFORM_SUPER_POST.equals(platformPost.getPostCode())) {
                throw ServiceException.build("标识为 PLATFORM_SUPER_POST 的岗位不能删除");
            }
            if (Constant.SYSTEM_SUPER_POST.equals(platformPost.getPostCode())) {
                throw ServiceException.build("标识为 SYSTEM_SUPER_POST 的岗位不能删除");
            }
        });

        List<PlatformUserPost> platformUserPostList = platformUserPostService.list(new LambdaQueryWrapper<PlatformUserPost>().in(PlatformUserPost::getPostId, postIdList));
        if (ObjectUtil.isNotEmpty(platformUserPostList)) {
            platformPostList.forEach(platformPost -> {
                List<PlatformUserPost> platformUserPosts = new ArrayList<>();
                platformUserPostList.forEach(platformUserPost -> {
                    if (platformPost.getPostId().equals(platformUserPost.getPostId())) {
                        platformUserPosts.add(platformUserPost);
                    }
                });
                platformPost.setPlatformUserPosts(platformUserPosts);
            });

            platformPostList.forEach(platformPost -> {
                List<PlatformUserPost> platformUserPosts = platformPost.getPlatformUserPosts();
                if (ObjectUtil.isNotEmpty(platformUserPosts)) {
                    throw ServiceException.build(platformPost.getPostName() + "该岗位已被用户绑定不能删除");
                }
            });
        }
        // 删除岗位
        return removeBatchByIds(postIdList);
    }

    /**
     * 修改岗位信息
     *
     * @param platformPostEditVo 岗位视图对象
     * @return 是否成功
     */
    @Override
    public Boolean edit(PlatformPostEditVo platformPostEditVo) {
        if (ObjectUtil.isEmpty(platformPostEditVo)) {
            throw ServiceException.build("岗位对象无效");
        }

        PlatformPost platformPost = PlatformPostBasic.INSTANCE.toPlatformPost(platformPostEditVo);
        if (ObjectUtil.isEmpty(platformPost)) {
            throw ServiceException.build("岗位对象无效");
        }

        PlatformPost dbPlatformPost = getOne(new LambdaQueryWrapper<PlatformPost>().eq(PlatformPost::getPostId, platformPost.getPostId()));
        if (ObjectUtil.isEmpty(dbPlatformPost)) {
            throw ServiceException.build(platformPost.getPostName() + "该岗位在数据库中不存在");
        }

        if (Constant.PLATFORM_SUPER_POST.equals(dbPlatformPost.getPostCode()) && !dbPlatformPost.getPostCode().equals(platformPost.getPostCode())) {
            throw ServiceException.build("标识为 PLATFORM_SUPER_POST 的岗位标识不能修改");
        }

        if (Constant.PLATFORM_SUPER_POST.equals(dbPlatformPost.getPostCode()) && !dbPlatformPost.getStatus().equals(platformPost.getStatus())) {
            throw ServiceException.build("标识为 PLATFORM_SUPER_POST 的岗位状态不能修改");
        }

        if (Constant.SYSTEM_SUPER_POST.equals(dbPlatformPost.getPostCode()) && !dbPlatformPost.getPostCode().equals(platformPost.getPostCode())) {
            throw ServiceException.build("标识为 SYSTEM_SUPER_POST 的岗位标识不能修改");
        }

        if (Constant.SYSTEM_SUPER_POST.equals(dbPlatformPost.getPostCode()) && !dbPlatformPost.getStatus().equals(platformPost.getStatus())) {
            throw ServiceException.build("标识为 SYSTEM_SUPER_POST 的岗位状态不能修改");
        }

        // Code 必须唯一
        if (!platformPost.getPostCode().equals(dbPlatformPost.getPostCode())) {
            PlatformPost dbPlatformPostByCode = getOne(new LambdaQueryWrapper<PlatformPost>().eq(PlatformPost::getDeptId, platformPost.getDeptId()).eq(PlatformPost::getPostCode, platformPost.getPostCode()));
            if (ObjectUtil.isNotEmpty(dbPlatformPostByCode)) {
                throw ServiceException.build(platformPost.getPostName() + "该岗位的标识重复");
            }
        }

        // name 必须唯一
        if (!platformPost.getPostName().equals(dbPlatformPost.getPostName())) {
            PlatformPost dbPlatformPostByName = getOne(new LambdaQueryWrapper<PlatformPost>().eq(PlatformPost::getDeptId, platformPost.getDeptId()).eq(PlatformPost::getPostName, platformPost.getPostName()));
            if (ObjectUtil.isNotEmpty(dbPlatformPostByName)) {
                throw ServiceException.build(platformPost.getPostName() + "该岗位的名称重复");
            }
        }

        BeanUtil.copyProperties(platformPost, dbPlatformPost, true);
        return updateById(dbPlatformPost);
    }

    /**
     * 根据ID获取岗位详情
     *
     * @param postId 岗位ID
     * @return 岗位详情
     */
    @Override
    public PlatformPostQueryDto info(@NotBlank(message = "岗位ID不能为空") String postId) {
        PlatformPost platformPost = getOne(new LambdaQueryWrapper<PlatformPost>().eq(PlatformPost::getPostId, postId));
        return PlatformPostBasic.INSTANCE.toPlatformPostQueryDto(platformPost);
    }

    /**
     * 分页查询岗位列表
     *
     * @param platformPostQueryVo 岗位视图对象
     * @return 分页岗位列表
     */
    @Override
    @DataScope
    public PageData<PlatformPostQueryDto> pageList(PlatformPostQueryVo platformPostQueryVo) {
        Page<PlatformPost> page = startPage();
        List<PlatformPost> platformRoleList = platformPostMapper.pageList(PlatformPostBasic.INSTANCE.toPlatformPost(platformPostQueryVo), page);
        return PageData.build(new Page<>(page.getCurrent(), page.getSize(), page.getTotal()), PlatformPostBasic.INSTANCE.toPlatformPostQueryDtoList(platformRoleList));
    }

}
